package com.murphy.springmvc.handler;

import com.murphy.springmvc.exception.BusinessExcetpion;
import com.murphy.springmvc.exception.LoginException;
import com.murphy.springmvc.model.ResponseDTO;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;

/**
 * @author moqinc
 * @date 2019/12/18 16:50
 * @version 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @SuppressWarnings("all")
    private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BusinessExcetpion.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResponseDTO<Boolean> verifyErrorHandler(HttpServletRequest request, Exception e) {
        log.info("BusinessExcetpion={}", e.getMessage(), e);
        return new ResponseDTO<Boolean>().build("100001", e.getMessage(), Boolean.TRUE);
    }

    @ExceptionHandler(LoginException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public ResponseDTO<Boolean> verifyLoginErrorHandler(HttpServletRequest request, Exception e) {
        log.info("LoginException={}", e.getMessage(), e);
        return new ResponseDTO<Boolean>().build("200001", e.getMessage(), Boolean.TRUE);
    }
}
